package com.nativetalk.service.three.comment;

import com.nativetalk.base.Page;
import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.TdNtSystemMessage;
import com.nativetalk.bean.comment.TdNtCommentCouse;
import com.nativetalk.bean.comment.TdNtCommentPage;
import com.nativetalk.bean.comment.TdNtTeacherComment;
import com.nativetalk.common.Constants;
import com.nativetalk.common.ValidationMessages;
import com.nativetalk.common.util.CommonUtil;
import com.nativetalk.dao.*;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.*;

/**
 * Created by lizhun on 16/8/23.
 */
@Service
public class ThreeCommentServiceImpl implements ThreeCommentService{
    private static Logger logger = Logger.getLogger(ThreeCommentServiceImpl.class);
    @Resource
    private TdNtCommentDao tdNtCommentDao;

    @Resource
    private TdNtStudyTeachDao tdNtStudyTeachDao;

    @Resource
    private TdNtTeacherCommentDao tdNtTeacherCommentDao;

    @Resource
    private TdNtSystemMessageDao tdNtSystemMessageDao;

    @Resource
    private TdNtReserveDao tdNtReserveDao;

    @Override
    public RetInfo findCommentByTeaId(Page page) {
        String logInfo = this.getClass().getName() + ":findCommentByTeaId:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            Map<String, Object> result = new HashMap<>();
            int currentPage = page.getCurrentPage();
            int pageSize = page.getPageSize();
            int totalPage;//总页数
            int totalRecord = tdNtCommentDao.findTdNtCommentByTeaIdSize(page);//总条数
            if (totalRecord > 0) {
                if (totalRecord % pageSize == 0) {
                    totalPage = totalRecord / pageSize;
                } else {
                    totalPage = (totalRecord / pageSize) + 1;
                }
                if (currentPage >= totalPage) {
                    currentPage = totalPage;
                }
                if (currentPage <= 1) {
                    page.setCurrentPage(1);
                }
                page.setCurrentPage((page.getCurrentPage()-1)*pageSize);
                List<TdNtCommentCouse> tdNtCommentCouses = tdNtCommentDao.findTdNtCommentByTeaIdPage(page);
                for (int j=0 ; j < tdNtCommentCouses.size() ; j ++ ) {

                    TdNtCommentCouse tdNtCommentCouse = tdNtCommentCouses.get(j);
                    String name = CommonUtil.substring(tdNtCommentCouse.getMemb_name(),0,1) + "****" +
                            CommonUtil.substring(tdNtCommentCouse.getMemb_name(),tdNtCommentCouse.getMemb_name().length() - 1,tdNtCommentCouse.getMemb_name().length());
                    tdNtCommentCouse.setMemb_name(name);
                }
                result.put("comments", tdNtCommentCouses);
                result.put("totalPage", totalPage);

                retInfo.setMark(ValidationMessages.getValue("MARKYES"));
                retInfo.setTip("Success");
            } else {
                result.put("comments", new ArrayList<>());
                result.put("totalPage", 0);
                retInfo.setMark(ValidationMessages.getValue("MARKYES"));
                retInfo.setTip("No data");
            }
            retInfo.setObj(result);
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    @Transactional
    public RetInfo teahcerComment(TdNtTeacherComment tdNtTeacherComment) {
        String logInfo = this.getClass().getName() + ":findCommentByTeaId:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            TdNtCommentPage tdNtCommentPage = tdNtReserveDao.findReserverById(tdNtTeacherComment.getReserve_id());
            logger.info("!!!!!!!!!!!!!!!!!!!!!!!!" + (tdNtCommentPage.getTea_id() == tdNtTeacherComment.getTea_id()) + "");
            if (tdNtCommentPage != null && tdNtCommentPage.getTea_id().equals(tdNtTeacherComment.getTea_id())){
                tdNtTeacherComment.setCom_month(CommonUtil.getMonth(new Date()));
                tdNtTeacherComment.setCou_id(tdNtCommentPage.getCou_id());
                tdNtTeacherComment.setCou_valid(1);
                tdNtTeacherComment.setMemb_id(tdNtCommentPage.getMemb_id());
                tdNtTeacherComment.setTea_id(tdNtCommentPage.getTea_id());
                tdNtTeacherCommentDao.insertTdNtTeacherComment(tdNtTeacherComment);

                tdNtReserveDao.updateTeaIsComment(1,tdNtTeacherComment.getReserve_id());

                // 如果评价的是体验课，则发送系统消息，否则不发送系统消息
                if (tdNtTeacherComment.getIs_trial().intValue() == 1) {
                    logger.info("老师评价的是体验课，发送系统消息");
                    //查看系统消息表中是否已存在该学生的评估报告
                    TdNtSystemMessage tdNtSystemMessage = tdNtSystemMessageDao.findUserMessageSize(tdNtTeacherComment.getMemb_id());
                    TdNtSystemMessage msg = new TdNtSystemMessage();
                    msg.setSys_publish_time(new Timestamp(System.currentTimeMillis()));
                    if (tdNtSystemMessage == null) {
                        logger.info("第一次评价体验课，生成一条新的系统消息");
                        // 添加语言能力测评报告。如果老师评价的是体验课，则生成语言能力测评报告消息。学生端查询SystemMessage查看测评报告
                        msg.setSys_mes_title("语言能力测评报告");
                        msg.setSys_mes_banner("http://file.waijiaojun.com/waijiaojun/app/image/system-message/%E8%83%BD%E5%8A%9B%E6%B5%8B%E8%AF%95%E6%8A%A5%E5%91%8A%E6%B6%88%E6%81%AF%E5%B0%81%E9%9D%A2.jpg");
                        msg.setSys_mes_type(Constants.SystemMessage.SysMsgType.STUDENT);
                        msg.setSys_mes_url("/JspPage/" + tdNtCommentPage.getMemb_id() + "/member/return/reportPage");
                        msg.setUser_id(tdNtCommentPage.getMemb_id());

                        tdNtSystemMessageDao.insert(msg);
                    } else {
                        logger.info("评价体验课>1次，更新系统消息");
                        msg.setSys_mes_id(tdNtSystemMessage.getSys_mes_id());
                        tdNtSystemMessageDao.updateUserMessageDate(msg);
                    }
                } else {
                    logger.info("评价的不是体验课，不发送系统消息");
                }


                retInfo.setMark(ValidationMessages.getValue("MARKYES"));
                retInfo.setTip("成功");
            } else {
                retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                retInfo.setTip("课程不存在或已评论");
            }
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }


}
